<?php
/**
 * Serverlist Instance class
 *
 * Use this class to initialize all connected servers
 *
 * PHP version 5
 *
 * Copyright (C) 2012 Ewald van Gemert (ewald@vangee.nl)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * @category   Development tool
 * @package    MySQLcompare
 * @author     Ewald van Gemert <ewald@vangee.nl>
 * @license    MIT 3
 * @version    SVN: 
 * @link       http://code.google.com/p/mysqlcompare
 * @since      File available since Release 0.9
 */

// ###########################################################################
// Class van de Serverlijst
// ###########################################################################
class SRV {
	public $servers = array();
	private $setting = array();
	private $defaultSrv = "";
	
	private function setDefault($srv) {
		if ($this->defaultSrv=="") $this->defaultSrv = $srv;
	}

	private function add($filecontent) {
		$si = new SRVITEM($filecontent, $this->setting);
		$this->servers[$si->identifier()] = $si;
		$this->setDefault($si->identifier());
	}

	// ***********************************************************************
	// public functions
	// ***********************************************************************
	public function __construct($setting) {
		$this->setting = $setting;
		$path = "{$setting['base']}/{$setting['serverconfigfolder']}";
		
		$arr = array_diff(scandir("{$path}"), array('.','..'));
		foreach ($arr as $file) if (is_file("{$path}/{$file}")) {
			include "{$path}/{$file}";
			$this->add($server);
		}
	}
	
	function srvoptions($selected=null) {
		$options = "";
		foreach ($this->servers as $key => $val) {
		  $options .= "<option value=\"{$key}\"".(($selected==$key)?" selected":"").">{$val->identifier()}</option>";
		}
		return $options;
	}
	
	function si($identifier) {
		return (isset($this->servers[$identifier])) ? $this->servers[$identifier] : $this->servers[$this->defaultSrv];
	}

	function gettables($srvdb) {
		list($server, $db) = explode(".", $srvdb);
		if (!isset($this->servers[$server])) exit("Server illigal");
		return $this->servers[$server]->gettables($db);
	}
	
}
